<?php

using('Library.Widgets.Form');
using('Library.Widgets.RecordSet');

class Module_Admin_users extends Admin_IModule
{
    var $_Name  = 'users';
    var $_Title = 'Users';
    
    /**#@+
     * Required Core Objects
     *
     * These are references to the core objects required by this
     * class.
     *
     * @access private
     * @var    object
     */
    var $_Error;
    var $_Profiler;
    var $_DAL;
    var $_Model;
    /**#@-*/

    function __construct()
    {
        // Get references to required objects
        $this->_Error     = &Vault::Get('Debug_Error');
        $this->_Profiler  = &Vault::Get('Debug_Profiling');
        $this->_DAL       = &Vault::Get('DAL');
        $this->_Model     = &Vault::Get('Model');

        // Register User Specific Actions
        $this->RegisterAction(array('user'),           'Users', 'ManageUsers_List');
        $this->RegisterAction(array('user', 'create'), 'Create',       'ManageUsers_Create');
        $this->RegisterAction(array('user', 'modify'), 'Modify',       'ManageUsers_Modify');
        $this->RegisterAction(array('user', 'delete'), 'Delete',       'ManageUsers_Delete');

        // Register Group Specific Actions
        $this->RegisterAction(array('group'),           'Groups', 'ManageGroups_List');
        $this->RegisterAction(array('group', 'create'), 'Create',        'ManageGroups_Create');
        $this->RegisterAction(array('group', 'modify'), 'Modify',        'ManageGroups_Modify');
        $this->RegisterAction(array('group', 'delete'), 'Delete',        'ManageGroups_Delete');
        
        // Call Admin_IModule constructor (we call it last so it has access to the actions array)
        parent::__construct();        
    }

    function ManageUsers_List()
    {
        $rs = new RecordSet('user', 'Current Users', 'user', 20);
        $rs->SearchFields('name', 'email');
        $rs->AddField('id', '#', 20);
        $rs->AddField('email', 'E-Mail', 100);
        $rs->AddField('group_label', 'Group', 100);
        $rs->AddAction('modify', array(&$this, 'ManageUsers_Modify'));
        $rs->AddAction('delete', $this->GetAction('user', 'delete'));
        $this->_Model->Body('library', $rs->Render(), 'users', 'recordset');
    }
    
    function ManageUsers_Create()
    {
        /*
         * Generate the create user form before going any further.
         */

        $form = new Form('create_user', $this->GetAction('user', 'create'));
        $form->Add(new HeaderField('User Details'));
        $form->Add(new TextField('username', 'Username', null, null, true));
        $form->Add(new PasswordField('password', 'Password', null, null, true));
        $form->Add(new PasswordField('verify', 'Verify', null, null, true));
        $form->Add(new ListField('gid', 'Group', 'group', false, true));
        $form->Add(new TextField('email', 'E-Mail', null, null, true));
        $form->Add(new TextField('name', 'Name', null, null, true));
        $form->Add(new TextField('displayname', 'Display Name', null, null, false));
        

        /*
         * Process the form ensuring it has been submitted and that the
         * data is valid.
         */
        
        if ($form->Submitted())
        {
            if (!$form->Validate())
                $this->_Model->Body('library', $form->ToArray(), 'user', 'form');
            else 
            {
                /*
                 * Form submitted with valid data, lets create the new user
                 */
                
                $this->_DAL->Insert('user', array('gid'          => $_POST['gid'],
                                                  'status'       => '2',
                                                  'username'     => $_POST['username'],
                                                  'email'        => $_POST['email'],
                                                  'password'     => $_POST['password'],
                                                  'name'         => $_POST['name'],
                                                  'display_name' => $_POST['displayname'],
                                                  'theme'        => '',
                                                  'signature'    => '',
                                                  'timezone'     => '',
                                                  'language'     => '',
                                                  'avatar'       => ''));
                   
            }            
        }
        else 
            $this->_Model->Body('library', $form->ToArray(), 'user', 'form');
    }
    
    function ManageUsers_Modify()
    {
        /*
         * Grab the data of the user to be modified.
         */
        
        $user = $this->_DAL->Select('user', $this->_Model->Get('Request', 'uid'));
        
        /*
         * Generate the create user form before going any further.
         */

        $form = new Form('create_user', $this->GetAction('user', 'create'));
        $form->Add(new HeaderField('User Details'));
        $form->Add(new TextField('username', 'Username', null, $user->username, true));
        $form->Add(new PasswordField('password', 'Password', null, null, false));
        $form->Add(new PasswordField('verify', 'Verify', null, null, false));
        $form->Add(new ListField('gid', 'Group', 'group', $user->gid, true));
        $form->Add(new TextField('email', 'E-Mail', null, $user->email, true));
        $form->Add(new TextField('name', 'Name', null, $user->name, true));
        $form->Add(new TextField('displayname', 'Display Name', null, $user->display_name, false));
        

        /*
         * Process the form ensuring it has been submitted and that the
         * data is valid.
         */
        
        if ($form->Submitted())
        {
            if (!$form->Validate())
                $this->_Model->Body('library', $form->ToArray(), 'user', 'form');
            else 
            {
                /*
                 * Form submitted with valid data, lets update the users details
                 */
                
                $user->username     = $_POST['username'];
                $user->gid          = $_POST['gid'];
                $user->email        = $_POST['email'];
                $user->name         = $_POST['name'];
                $user->display_name = $_POST['displayname'];                  
                $user->Save();
            }            
        }
        else 
            $this->_Model->Body('library', $form->ToArray(), 'user', 'form');        
    }
    
    function ManageUsers_Delete()
    {
        $this->_DAL->Delete('user', $this->_Model->Get('Request', 'uid'));
        $this->ManageUsers_List();
    }
    
    function ManageGroups_List()
    {
        $rs = new RecordSet('group', 'Groups', 'group', 20);
        $rs->AddField('id', '#');
        $rs->AddField('label', 'Label');
        $rs->AddField('description', 'Description');
        $rs->AddAction('modify', $this->GetAction('group', 'modify'));
        $rs->AddAction('delete', $this->GetAction('group', 'delete'));
        $this->_Model->Body('library', $rs->Render(), 'groups', 'recordset');        
    }
    
    function ManageGroups_Create()
    {
        /*
         * Generate the create user form before going any further.
         */

        $form = new Form('create_user', $this->GetAction('group', 'create'));
        $form->Add(new HeaderField('Group Details'));
        $form->Add(new TextField('name', 'Name', null, null, true));
        $form->Add(new TextField('description', 'Description', null, null, true));        

        /*
         * Process the form ensuring it has been submitted and that the
         * data is valid.
         */
        
        if ($form->Submitted())
        {
            if (!$form->Validate())
                $this->_Model->Body('library', $form->ToArray(), 'group', 'form');
            else 
            {
                /*
                 * Form submitted with valid data, lets create the new user
                 */
                
                $this->_DAL->Insert('user', array('label'       => $_POST['name'],
                                                  'description' => $_POST['description']));                   
            }            
        }
        else 
            $this->_Model->Body('library', $form->ToArray(), 'group', 'form');        
    }
    
    function ManageGroups_Modify()
    {
        /*
         * Grab the data of the group to be modified.
         */
        
        $group = $this->_DAL->Select('group', $this->_Model->Get('Request', 'gid'));
        
        /*
         * Generate the group form before going any further.
         */

        $form = new Form('create_group', $this->GetAction('group', 'create'));
        $form->Add(new HeaderField('Group Details'));
        $form->Add(new TextField('name', 'Name', null, $group->label, true));
        $form->Add(new TextField('description', 'Description', null, $group->description, true));
        

        /*
         * Process the form ensuring it has been submitted and that the
         * data is valid.
         */
        
        if ($form->Submitted())
        {
            if (!$form->Validate())
                $this->_Model->Body('library', $form->ToArray(), 'group', 'form');
            else 
            {
                /*
                 * Form submitted with valid data, lets update the group details
                 */
                
                $group->label       = $_POST['name'];
                $group->description = $_POST['description'];                 
                $group->Save();
            }            
        }
        else 
            $this->_Model->Body('library', $form->ToArray(), 'group', 'form');           
    }
    
    function ManageGroups_Delete()
    {
        $this->_DAL->Delete('group', $this->_Model->Get('Request', 'uid'));
        $this->ManageGroups_List();        
    }
}

?>